package com.sgb.kjwl.view.ui.adapter.enterprise;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemEnterpriseSingleVideoBinding;
import com.sgb.kjwl.databinding.ItemEnterpriseZhuanyebaojiaBinding;
import com.sgb.kjwl.model.entity.enterprise.EpQuoteEntity;
import com.sgb.knowledgeshare.view.activity.PlayListActivity;
import com.swgk.core.base.model.entity.VideoBean;
import com.sgb.kjwl.model.entity.transport.SingleItemConstant;

import com.sgb.kjwl.view.ui.adapter.transport.GoodsInfoVideoAdapter;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.PointUtils;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class HomeQuoteAdapter extends BaseMultiItemQuickAdapter<EpQuoteEntity, BaseViewHolder> {

    private String subStr;

    public HomeQuoteAdapter(List<EpQuoteEntity> datas) {
        super(datas);
        addItemType(SingleItemConstant.TYPE_ONE, R.layout.item_enterprise_zhuanyebaojia);
        //视频类别
        addItemType(SingleItemConstant.TYPE_TWO, R.layout.item_enterprise_video_list);
        //单个视频类别
        addItemType(SingleItemConstant.TYPE_THREE, R.layout.item_enterprise_single_video);
    }

    @Override
    protected void convert(BaseViewHolder helper, EpQuoteEntity item) {
        try {
            switch (item.getItemType()) {
                case SingleItemConstant.TYPE_ONE:
                    setService(helper, item);
                    break;
                case SingleItemConstant.TYPE_TWO:
                    initVideoList(helper, item);
                    break;
                case SingleItemConstant.TYPE_THREE:
                    initSingleVideo(helper, item);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void setService(BaseViewHolder holder, EpQuoteEntity epQuoteEntity) {
        ItemEnterpriseZhuanyebaojiaBinding binding = DataBindingUtil.bind(holder.itemView);
        binding.setViewModel(epQuoteEntity);
        binding.btnChat.setOnClickListener(v -> iCallPhone.call(epQuoteEntity.getContactPhone(), epQuoteEntity.getContact()));
        binding.tvTime.setText(DateUtils.timestampTo13Date3(epQuoteEntity.getUpdateTime()));
        if (epQuoteEntity.getFirstCategoryName() != null && !epQuoteEntity.getFirstCategoryName().equals("")) {
            subStr = epQuoteEntity.getFirstCategoryName();
            if (epQuoteEntity.getSecondCategoryName() != null && !epQuoteEntity.getSecondCategoryName().equals("")) {
                String two = epQuoteEntity.getSecondCategoryName();
                subStr = subStr + "-" + two;
            }
            if (epQuoteEntity.getItemName() != null && !epQuoteEntity.getItemName().equals("")) {
                String three = epQuoteEntity.getItemName();
                subStr = subStr + "-" + three;
            }
        }
        binding.tvType1.setVisibility(View.VISIBLE);
        switch (epQuoteEntity.getPublishType()) {
            case 0:
                binding.tvType1.setText("个人");
                binding.tvType1.setBackgroundResource(R.drawable.bg_card_type_authen);
                break;
            case 1:
                binding.tvType1.setText("企业");
                binding.tvType1.setBackgroundResource(R.drawable.bg_card_type_authen);
                break;
            case 2:
                binding.tvType1.setText("团队");
                binding.tvType1.setBackgroundResource(R.drawable.bg_team_authen);
                break;
            default:
                binding.tvType1.setVisibility(View.GONE);
                break;
        }
        binding.tvCategory.setText(subStr);

        if(!TextUtils.isEmpty(epQuoteEntity.getDomesticCompaniesPrice())&&!TextUtils.isEmpty(epQuoteEntity.getMaximumPrice())){
            if(epQuoteEntity.getDomesticCompaniesPrice().equals("-1")) {
                binding.priceQuote.setText("议价");
            }else{
                binding.priceQuote.setText("¥"+ PointUtils.price(Double.parseDouble(epQuoteEntity.getDomesticCompaniesPrice()))+"起");
            }
        }else{
            binding.priceQuote.setText(epQuoteEntity.getPrice()!=-1?TextViewBindingAdapter.setMoneyForRMB("¥"+PointUtils.price(epQuoteEntity.getPrice())):"议价");
        }
        String cities = epQuoteEntity.getServiceCitys();
        if (TextUtils.isEmpty(cities)) {
            binding.tvServiceCities.setText("/");
        } else {
            ArrayList<BaseAreaEntity> citysList = jsonToArrayList(cities, BaseAreaEntity.class);
            StringBuilder stringBuilder = new StringBuilder();
            for (BaseAreaEntity entity : citysList) {
                stringBuilder.append(entity.getName()).append("/");
            }
            if (stringBuilder.toString().length() > 0) {
                String cityShow = stringBuilder.toString().substring(0, stringBuilder.toString().length() - 1);
                binding.tvServiceCities.setText(cityShow);
            }
        }
    }

    /**
     * 设置视频信息
     */
    private void initVideoList(BaseViewHolder helper, EpQuoteEntity entity) {
        List<VideoBean> videoBeanList = new ArrayList<>();
        videoBeanList.addAll(entity.getVideos());
        RecyclerView recyclerView = helper.getView(R.id.list_data);
        if (entity.getVideos() == null || entity.getVideos().size() == 0) {
            recyclerView.setVisibility(View.GONE);
            return;
        }
        recyclerView.setVisibility(View.VISIBLE);
        if (recyclerView.getAdapter() == null) {
            recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
            GoodsInfoVideoAdapter adapter = new GoodsInfoVideoAdapter(mContext, videoBeanList);
            adapter.setOnItemClick(new AdapterOnItemClick<VideoBean>() {
                @Override
                public void onItemClick(VideoBean homeHostVideoEntity, int position) {
                    Intent intent = new Intent(mContext, PlayListActivity.class);
                    intent.putExtra(PlayListActivity.VIDEOLISTBEAN, (Serializable) adapter.getDatas());
                    intent.putExtra(PlayListActivity.PSO, position);
                    intent.putExtra(PlayListActivity.VIDEOTYPE, "goods_and_car_video");
                    mContext.startActivity(intent);
                }
            });
            recyclerView.setAdapter(adapter);
        } else {
            ((GoodsInfoVideoAdapter) recyclerView.getAdapter()).setDatas(videoBeanList);
        }
    }

    /**
     * 设置单个视频信息
     */
    private void initSingleVideo(BaseViewHolder helper, EpQuoteEntity entity) {
        //设置dataBinding数据
        ItemEnterpriseSingleVideoBinding binding = DataBindingUtil.bind(helper.itemView);
        if (binding == null) return;
        if (entity.getVideos() != null && !entity.getVideos().isEmpty()) {
            binding.setVariable(BR.viewModel, entity.getVideos().get(0));
            binding.executePendingBindings();
        }
    }

    /**
     * @param json
     * @param clazz
     * @return
     */
    public static <T> ArrayList<T> jsonToArrayList(String json, Class<T> clazz) {
        Type type = new TypeToken<ArrayList<JsonObject>>() {
        }.getType();
        ArrayList<T> arrayList = new ArrayList<>();
        try {
            ArrayList<JsonObject> jsonObjects = new Gson().fromJson(json, type);

            for (JsonObject jsonObject : jsonObjects) {
                arrayList.add(new Gson().fromJson(jsonObject, clazz));
            }
        } catch (Exception e) {

        }

        return arrayList;
    }

    private ICallPhone iCallPhone;

    public interface ICallPhone {
        void call(String num, String name);
    }

    public void OnCallPhone(ICallPhone iCallPhone) {
        this.iCallPhone = iCallPhone;
    }
}
